package com.tensquare.friend.controller;

import com.tensquare.entity.Result;
import com.tensquare.entity.StatusCode;
import com.tensquare.friend.client.UserClient;
import com.tensquare.friend.service.FriendService;
import com.tensquare.util.NotNullHelps;
import io.jsonwebtoken.Claims;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;


@Slf4j
@CrossOrigin
@RestController
@RequestMapping("/friend")
public class FriendController {

    private Result result = new Result(true, StatusCode.OK);

    @Autowired
    private HttpServletRequest request;

    @Autowired
    private FriendService friendService ;

    @Autowired
    private UserClient userClient ;

    private String message ;

    private Integer flag ;

    protected String getUserIdFromRequest(){
        String userId = null ;
        Claims claims = (Claims) request.getAttribute("claims_user");
        if (NotNullHelps.isNotNull(claims) == true) {
            userId = claims.getId();
        }
        return userId;
    }

    @RequestMapping(value = "/{friendId}",method = RequestMethod.DELETE)
    public Result deleteFriend(@PathVariable String friendId){
        String userId = this.getUserIdFromRequest();
        if (NotNullHelps.isNotNull(userId) == false) {
            result = new Result(false, StatusCode.ACCESSERROR);
            message = "$$ 权限不足/过大 @@";
        } else {
            friendService.deleteFriend(userId, friendId);
            userClient.updateFanAndFollowCount(userId, friendId, -1);
        }
        result.setMessage(message);
        return result ;
    }

    @RequestMapping(value = "/like/{friendId}/{type}" , method = RequestMethod.PUT)
    public Result addFriend(@PathVariable String friendId , @PathVariable String type ){
        if (NotNullHelps.checkNotNull(friendId, type) == false) {
            throw new RuntimeException("参数异常");
        } else {
            String userId = this.getUserIdFromRequest();
            if (NotNullHelps.isNotNull(userId) == false) {
                result = new Result(false, StatusCode.ACCESSERROR);
                message = "$$ 权限不足/过大 @@";
            } else {
                if ("1".equals(type) == true) {
                    flag = friendService.addFriend(userId, friendId);
                    if (flag != 1) {
                        result = new Result(false, StatusCode.ERROR);
                        message = "添加关注--不可重复";
                    } else {
                        userClient.updateFanAndFollowCount(userId, friendId, 1);
                        message = "关注成功";
                    }
                } else if ("2".equals(type)) {
                    flag = friendService.addNoFriend(userId, friendId);
                    if (flag != 1) {
                        result = new Result(false, StatusCode.ERROR);
                        message = "已经不关注，不需重复";
                    } else {
                        message = "成功不关注";
                    }
                }
            }
        }
        result.setMessage(message);
        return result ;
    }


}
